package com.ebt.m.proposal_v2.ui;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import com.ebt.m.R;
import com.ebt.m.data.bean.Brand;
import com.ebt.m.proposal_v2.adapter.BrandAdapter;
import com.ebt.m.proposal_v2.dao.ProposalApi;
import com.ebt.m.proposal_v2.dao.request.ParamHistory;
import com.ebt.m.proposal_v2.dao.response.ResponseBrand;
import com.ebt.m.proposal_v2.dao.response.ResponseHistory;
import com.ebt.m.proposal_v2.event.BrandFilterEvent;
import com.ebt.m.proposal_v2.helper.PickerHelper;
import com.ebt.m.proposal_v2.retrofit.OnResponseListener;
import com.ebt.m.proposal_v2.retrofit.ResponseError;
import com.ebt.m.proposal_v2.retrofit.ResponsePage;
import com.ebt.m.proposal_v2.utils.DataUtils;
import com.ebt.m.proposal_v2.widget.dialog.EBTProgressDialog;
import com.ebt.m.wiki.SpecProductFilterView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 筛选品牌菜单
 *
 * @author archer.qi
 *         Created on 2017/5/26.
 */
public class FragmentBrandMenu extends Fragment {

    private Context mApplicationContext;
    private OnBrandSelectListener mListener;
    private Brand mCurrentChecked;
    @BindView(R.id.recycler_view)
    SpecProductFilterView filterView;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View contentView = inflater.inflate(R.layout.product_spec_lib_filter, container, false);
        ButterKnife.bind(this, contentView);
        mApplicationContext = getActivity().getApplicationContext();
        return contentView;
    }

    @Override
    public void onResume() {
        super.onResume();
        filterView.post(() -> filterView.update(mCurrentChecked, 4));
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden)
            filterView.post(() -> filterView.update(mCurrentChecked, 4));
    }

    public void clearBrandChecked() {
        mCurrentChecked = null;
    }

    public void setOnBrandSelectListener(OnBrandSelectListener listener) {
        mListener = listener;
    }

    public interface OnBrandSelectListener {
        void onBrandSelect(ResponseBrand data);
    }


    @Subscribe
    public void onBrandSelectedEvent(BrandFilterEvent filterEvent) {
        if (mListener != null) {
            mCurrentChecked = filterEvent.brand;
            ResponseBrand brand = new ResponseBrand();
            brand.brandId = filterEvent.brand.getBrandId();
            brand.brandName = filterEvent.brand.getBrandName();
            brand.brandShortName = filterEvent.brand.getBrandShortName();
            brand.from = 4;
            mListener.onBrandSelect(brand);
        }
    }
}
